import { enemyPlane } from "../component/enemyPlane";

const { ccclass, property } = cc._decorator;

@ccclass
export default class enemyPlane_1 extends enemyPlane {
    @property(sp.Skeleton)
    spShiTou: sp.Skeleton = null;
    @property(cc.Integer)
    minAngle = 0;
    @property(cc.Integer)
    bulletAngle = 0;
    isTrack = false;//是否跟踪玩家
    initData() {
        if (this.node.name.indexOf("13") != -1) this.changeInvincibleState();
        if (this.isAttack) this.sendAttack();
        if (this.isMove) {
            this.scheduleMoveNode();
        }
    }

    initEventListener() {

    }

    scheduleMoveNode() {
        let self = this;
        this.scheduleOnce(() => {
            self.setNodeNextPosi();
        }, this.moveTime);
    }

    setNodeNextPosi() {
        let posi = this.node.position;
        let x = Math.round(Math.random() * 100) + 50;
        let y = Math.round(Math.random() * 100) + 50;
        let next_y = posi.y - y;
        let fangXiang = Math.random();
        let next_x = fangXiang > 0.5 ? posi.x + x : posi.x - x;
        this.nodeMoveEndPosi(cc.v3(next_x, next_y));
    }

    nodeMoveEndPosi(posi) {
        if (this.node.name.indexOf("6") != -1) {
            this.spShiTou.setAnimation(1, "run", false);
        }
        cc.tween(this.node)
            .to(0.2, { position: posi })
            .call(() => {
                if (this.node.name.indexOf("6") != -1) {
                    this.spShiTou.setAnimation(1, "idle", true);
                }
                if (this.node.y < -(cc.winSize.height / 2 + 500)) {
                    let size = cc.winSize;
                    let fangXiang = Math.random();
                    let randPosi = Math.random() * (size.width / 2) - 80;
                    let posi = fangXiang > 0.5 ? randPosi : -randPosi;
                    this.node.setPosition(cc.v2(posi, (size.height / 2) + 100));
                }
                else {
                    this.scheduleMoveNode();
                }
            })
            .start();
    }

    sendAttack() {
        let self = this;
        this.schedule(() => {
            self.getBullet(self.bulletNum, this.minAngle, this.bulletAngle, this.nodeBullet);
        }, this.attackTime)
    }

    changeInvincibleState() {
        this.schedule(this.invincibleState.bind(this), 8);
    }

    isInvincible = false;
    invincibleState() {
        this.isInvincible = true;
        this.spShiTou.setAnimation(1, "atk", true);
        let self = this;
        this.scheduleOnce(() => {
            self.isInvincible = false;
            self.spShiTou.setAnimation(1, "idle", true);
        }, 2)
    }
}
